package com.moneyanalyzer.views.budget;

import java.util.List;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

import com.moneyanalyzer.R;
import com.moneyanalyzer.adapter.BudgetAdapter;
import com.moneyanalyzer.dao.BudgetDao;
import com.moneyanalyzer.dao.CategoryDao;
import com.moneyanalyzer.model.Budget;
import com.moneyanalyzer.model.Category;

public class FormBudgetFragment extends Fragment {

	

	private Budget budget;
	private BudgetAdapter budgetAdapter;
	
	
	
	

	public FormBudgetFragment(){
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		
		final View rootView = inflater.inflate(R.layout.form_new_budget, container, false);
		final LinearLayout layoutDataAlarm = (LinearLayout)rootView.findViewById(R.id.layoutDataAlarm);
		final EditText editTextPercentAlarm = (EditText) rootView.findViewById(R.id.percentalarm);
		final Button btnSave = (Button) rootView.findViewById(R.id.btnSave);
		final Spinner spinnerCategory = (Spinner)rootView.findViewById(R.id.spinnerBudgetCategory);
		final Switch switchAlert = (Switch)rootView.findViewById(R.id.swichBudgetAlarm);
		final EditText editValueBudget = (EditText) rootView.findViewById(R.id.value_budget);
		final EditText editNameBudget = (EditText) rootView.findViewById(R.id.name_budget);
		
		
		
		CategoryDao categoryDao = CategoryDao.getInstance();
		List<Category> categories = categoryDao.list(rootView.getContext());
		
		ArrayAdapter<Category> dataAdapter = null;
		if(categories != null && categories.size() > 0){
			dataAdapter = new ArrayAdapter<Category>(rootView.getContext(), android.R.layout.simple_spinner_dropdown_item, categories);
			spinnerCategory.setAdapter(dataAdapter);
		}
		
		if(budget == null){
			budget = new Budget();
			budget.setId(0);
		}else{
			if(budget.isAlert())
				layoutDataAlarm.setVisibility(View.VISIBLE);
			else
				layoutDataAlarm.setVisibility(View.INVISIBLE);
				editTextPercentAlarm.setText(String.valueOf(budget.getPercentAlert()));
				for (int i = 0; i < dataAdapter.getCount(); i++) {
					if(dataAdapter.getItem(i).getName().equals(budget.getCategory().getName())){
						spinnerCategory.setSelection(i);
						break;
					}
				}
				
				
				switchAlert.setChecked(budget.isAlert());
				editValueBudget.setText(String.valueOf(budget.getValue()));
				editNameBudget.setText(String.valueOf(budget.getName()));
		}
		

		spinnerCategory.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view,
					int position, long arg3) {
				Category categorySelected = null;
				if(adapterView != null && adapterView.getCount() > 0)
					categorySelected = (Category)adapterView.getItemAtPosition(position);
				
					budget.setCategory(categorySelected);
				
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		switchAlert.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				//Toast.makeText(getActivity().getApplicationContext(), ">>>>> "+isChecked, Toast.LENGTH_LONG).show();
				if(isChecked){
					layoutDataAlarm.setVisibility(View.VISIBLE);
				}else{
					layoutDataAlarm.setVisibility(View.INVISIBLE);
					editTextPercentAlarm.setText("");
				}
				budget.setAlert(isChecked);
			}
		});
		
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				BudgetDao budgetDao = BudgetDao.getInstance();
				
				try{
					budget.setName(editNameBudget.getText().toString());
					budget.setValue(Float.valueOf(editValueBudget.getText().toString()));
					budget.setCategory((Category)spinnerCategory.getSelectedItem());
					budget.setAlert(switchAlert.isChecked());
					if(switchAlert.isChecked()){
						budget.setPercentAlert(Integer.valueOf(editTextPercentAlarm.getText().toString()));
					}
				}catch(Exception e){
					Toast.makeText(getActivity().getApplicationContext(), "Por favor preencher todos os campos.", Toast.LENGTH_LONG).show();
				}
				
				
				
				if(budget.getName() != null && budget.getValue() != 0 && budget.getCategory() != null && budget.getCategory() != null ){
					if(budget.getId() == 0){
						budgetDao.insert(budget, rootView.getContext());
					}else{
						budgetDao.update(budget, rootView.getContext());
					}
					
					getFragmentManager().popBackStack();
					List<Budget> budgetsNew = budgetDao.list(rootView.getContext());
					if(budgetAdapter != null)
						budgetAdapter.setMyList(budgetsNew);
					else if(budgetAdapter == null && budgetsNew != null && budgetsNew.size() > 0)
						budgetAdapter = new BudgetAdapter(rootView.getContext(), budgetsNew);
						
				}else{
					Toast.makeText(getActivity().getApplicationContext(), "Por favor preencher todos os campos.", Toast.LENGTH_LONG).show();
				}
					
				
				
				
			}
		});
		
		return rootView;
	}

	public Budget getBudget() {
		return budget;
	}

	public void setBudget(Budget budget) {
		this.budget = budget;
	}

	
	public BudgetAdapter getBudgetAdapter() {
		return budgetAdapter;
	}

	public void setBudgetAdapter(BudgetAdapter budgetAdapter) {
		this.budgetAdapter = budgetAdapter;
	}
}
